<?php

namespace App\Http\Controllers;

use App\Http\Services\ConfigServices;
use App\Http\Services\ShareDiamondServices;
use App\Http\Services\ShareSendGoodsServices;
use App\Http\Services\UserServices;
use Illuminate\Support\Facades\Config;
use Laravel\Lumen\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use Mockery\CountValidator\Exception;

/*
 * 充值赠送钻石
 * */

class DiamondController extends BaseController
{
    protected $configServices;
    protected $userServices;
    protected $shareDiamondServices;
    protected $shareSendGoodsServices;
    protected $share_accumulate_money = 100;//累计充值 100元
    protected $prop_17 = 17;//足迹清除卡
    protected $prop_19 = 19;//甜蜜蛋糕
    protected $send_prop_num= 1;//赠送个数
    protected $award_diamond = 3;//返利奖励比例 5%


    public function __construct()
    {
        //监听充值
        $this->configServices = new ConfigServices();
        $this->userServices = new UserServices();
        $this->shareDiamondServices = new ShareDiamondServices();
        $this->shareSendGoodsServices = new ShareSendGoodsServices();
    }

    /*
     *  {"order_id":11973,"order_sn":"1909060426062019090611915","user_id":383,"rule_id":23,"diamond_amount":100000
     * ,"price":100000,"pay_type":5,"status":1,"create_timestamp":1567758366
     * ,"create_time":"2019-09-06 08:26:06","pay_timestamp":1567758366,
     * "out_trade_sn":"1909060426062019090611915","update_time":"2019-09-06 08:26:06",
     * "channel_id":"1000000","pearl_amount":0,"recharge_type":0,"consumer_type":2}
     * */

    public function assignAction($body)
    {
        $body_date = $body['pay_timestamp']; //充值
        $consumer_date = $body_date ?? time();
        $config = $this->configServices->getConfig(1);
        $checkDate = $this->configServices->checkConfigDate($config['share_date'], $consumer_date);
        if (!$checkDate) {
            write_log('shareConsumer', 'timeError', $body);//活动时间错误
            return false;
        }
        $this->listenUserRecharge($body);// 充值
        return true;
    }
//
//{"order_id":21669,"order_sn":"1912020953232019120217304","user_id":28961,"rule_id":13,"diamond_amount":600
//,"price":600,"pay_type":12,"status":1,"create_timestamp":1575294803,"create_time":"2019-12-02 13:53:23","pay_tim
//estamp":1575294821,"out_trade_sn":"191202215323582205878900","u
//pdate_time":"2019-12-02 13:53:41","channel_id":"700035","pearl_amount":0,"recharge_type":0}

    public function listenUserRecharge($body)
    {
        //获取监听数据
//        $body = json_decode($body, true);
//        $body['user_id'] = $request->input('user_id');
//        $body['diamond_amount'] = 10000;
//        $body['price'] = 100;
//        $body['pay_type'] = 1;
//        $body['recharge_type'] = 0;
        $user_id = $body['user_id'] ?? 0;
        $diamond_num = $body['diamond_amount'] ?? 0;
        $pay_type = $body['pay_type'];
        $recharge_type = $body['recharge_type'] ?? 1;

        if($recharge_type != 0){
            write_log('consumer', 'rechargeTypeError', json_encode($body));//金豆不计入
            return false;
        }
        if($pay_type == 5){
            write_log('consumer', 'payTypeError', json_encode($body));//代充不计入
            return false;
        }
        //支付类型 --- 0.支付宝 1.微信 2.苹果内购 3.android公号 4.IOS公众号 5.后台代充 6.支付宝wap端充值 7.微信wap端充值
        if (empty($user_id) || empty($diamond_num)) {
            write_log('consumer', 'bodyError', json_encode($body));//paytype错
            return false;
        }

        $recharge_money = $body['price'];
        //获取配置
        //查询用户注册时间
        $checkUser = $this->userServices->checkUserRegisterTime($user_id);
        //无用户信息
        write_log('consumer', 'checkUserError', $user_id);//验证用户信息日志
        write_log('consumer', 'checkUserErrorType', $checkUser);//验证用户信息日志
        if ($checkUser == true) {
            //获取用户关系
            $user = $this->userServices->showUserInvite($user_id);
            $first_data['award_diamond'] = $diamond_num * $this->award_diamond / 100;
            $first_data['user_id'] = $user[1]['invite_uid'];
            //发送接口请求
            $recharge = [
                'user_id' => $user[1]['invite_uid'],
                'diamond_num' => $first_data['award_diamond'],
                'msg' => '邀请奖励',
            ];
            $diamond_api = json_decode(curl_request(env('KK_API_HOST') . '/finance/diamond/recharge', $recharge), true);
            if ($diamond_api['code'] != 0) {
                //充值失败 销毁队列
                write_log('consumer', 'oneUserRechargeError', json_encode($diamond_api));
                return false;
            }
            //返回成功记录
            $this->shareDiamondServices->createDiamond($user_id, $user[1]['invite_uid'], 1, $recharge_money, $diamond_num, $first_data['award_diamond']);
            $msg_data_one['user_id'] = $user[1]['invite_uid'];
            $msg_data_one['msg'] = "您的邀请函奖励到账 钻石* " . $first_data['award_diamond'];
            curl_request(env('KK_API_HOST') . '/msg/text', $msg_data_one);
            return true;
        }

    }


}
